/**
 * Records.java
 * author: Zhiqiang Zhu
 * date: Nov/07/2020
 */
package records;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

/**
 * class Records
 * 
 * Extend the BankRecords class for data analysis.
 *
 */
public class Records extends BankRecords {

	// file for writing output
	static FileWriter fw = null;

	private static double femaleIncome;
	private static int femaleCount;

	private static double maleIncome;
	private static int maleCount;

	private static int numOfFemalesWithMortgageAndSaveAcct;

	private static int numMales1Car1ChildInnercity;
	private static int numMales1Car1ChildRural;
	private static int numMales1Car1ChildSuburban;
	private static int numMales1Car1ChildTown;

	public Records() {
		try {
			fw = new FileWriter("bankrecords.txt");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Records br = new Records();
		br.readData();

		CalculateFemaleAverageIncome();
		CountMales1Car1ChildSubRegions();
		
		System.out.println();
		System.out.println();
		System.out.println("Data analytic results:");
		System.out.println();

		System.out.printf("Average income Females: $%.2f\n", femaleIncome / femaleCount);
		System.out.printf("Average income Males: $%.2f\n", maleIncome / maleCount);
		System.out.println();

		System.out.printf("Num. of Females with Mortgage & saving account: %d", numOfFemalesWithMortgageAndSaveAcct);
		System.out.println();

		System.out.printf("Innercity region males with car & 1 child: %d.\n", numMales1Car1ChildInnercity);
		System.out.printf("Rual region males with car & 1 child: %d.\n", numMales1Car1ChildRural);
		System.out.printf("Suburban region males with car & 1 child: %d.\n", numMales1Car1ChildSuburban);
		System.out.printf("Town region males with car & 1 child: %d.\n", numMales1Car1ChildTown);

		try {
			fw.write("Data analytic results:");
			fw.write('\n');

			fw.write(String.format("Average income Females: $%.2f\n", femaleIncome / femaleCount));
			fw.write(String.format("Average income Males: $%.2f\n", maleIncome / maleCount));
			fw.write('\n');

			fw.write(String.format("Num. of Females with Mortgage & saving account: %d", numOfFemalesWithMortgageAndSaveAcct));
			fw.write('\n');

			fw.write(String.format("Innercity region males with car & 1 child: %d.\n", numMales1Car1ChildInnercity));
			fw.write(String.format("Rual region males with car & 1 child: %d.\n", numMales1Car1ChildRural));
			fw.write(String.format("Suburban region males with car & 1 child: %d.\n", numMales1Car1ChildSuburban));
			fw.write(String.format("Town region males with car & 1 child: %d.\n", numMales1Car1ChildTown));			
			fw.write('\n');
			fw.write("Name: Zhiqiang Zhu\n");
			fw.write("Date: Nov / 07 / 2020\n");
			
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static void CalculateFemaleAverageIncome() {
		BankRecords arrRcds[] = BankRecords.CopyOfRecords();

		Arrays.sort(arrRcds, new SexComparator());

		femaleIncome = 0.0;
		femaleCount = 0;
		maleIncome = 0.0;
		maleCount = 0;

		numOfFemalesWithMortgageAndSaveAcct = 0;

		for (int i = 0; i < arrRcds.length; i++) {
			if (arrRcds[i].getSex().equalsIgnoreCase("FEMALE")) {
				femaleCount++;
				femaleIncome += arrRcds[i].getIncome();

				if (arrRcds[i].getMortgage().equalsIgnoreCase("YES")
						&& arrRcds[i].getSave_act().equalsIgnoreCase("YES")) {
					numOfFemalesWithMortgageAndSaveAcct++;
				}
			} else if (arrRcds[i].getSex().equalsIgnoreCase("MALE")) {
				maleCount++;
				maleIncome += arrRcds[i].getIncome();
			} else {
				System.err.printf("Unrecognized gender field: %s", arrRcds[i].getSex());
			}
		}
	}

	private static boolean Check1Car1Child(BankRecords r) {
		return (r.getChildren() == 1 && r.getSex().equalsIgnoreCase("MALE") && r.getCar().equalsIgnoreCase("YES"));
	}

	private static void CountMales1Car1ChildSubRegions() {
		BankRecords arrRcds[] = BankRecords.CopyOfRecords();

		Arrays.sort(arrRcds, new LocationComparator());

		numMales1Car1ChildInnercity = 0;
		numMales1Car1ChildRural = 0;
		numMales1Car1ChildSuburban = 0;
		numMales1Car1ChildTown = 0;

		for (int i = 0; i < arrRcds.length; i++) {
			if (arrRcds[i].getRegion().equalsIgnoreCase("INNER_CITY") && Check1Car1Child(arrRcds[i])) {
				numMales1Car1ChildInnercity++;
			} else if (arrRcds[i].getRegion().equalsIgnoreCase("RURAL") && Check1Car1Child(arrRcds[i])) {
				numMales1Car1ChildRural++;
			} else if (arrRcds[i].getRegion().equalsIgnoreCase("SUBURBAN") && Check1Car1Child(arrRcds[i])) {
				numMales1Car1ChildSuburban++;
			} else if (arrRcds[i].getRegion().equalsIgnoreCase("TOWN") && Check1Car1Child(arrRcds[i])) {
				numMales1Car1ChildTown++;
			}
		}
	}

}
